package lxt.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.transaction.CannotCreateTransactionException;

import lxt.dao.UserDao;
import lxt.entity.UserInfo;
/**
 * 用户管理dao
 * @author Lit
 *
 */
public class UserDaoImpl extends ABseDaoImpl implements UserDao {

	@SuppressWarnings("unchecked")
	public UserInfo isExist(String name, String pwd) {
		UserInfo user = null;
		try
		{
			String hql = "Select u From UserInfo u Where u.username = binary(?) And u.password = binary(?)  ";
			List<UserInfo> lst = this.getHibernateTemplate().find(hql,new Object[]{name,pwd});
			if (lst != null && !lst.isEmpty())
			{
				user = lst.get(0);
			}
		} catch(CannotCreateTransactionException ex){
			throw new RuntimeException("数据库连接异常!");
		}catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return user;	
	}
	@SuppressWarnings("unchecked")
	public List<UserInfo> loadAllUser(String username, int offset, int length) {
		StringBuffer hql = new StringBuffer("Select u from UserInfo u where 1=1 ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(username!=null&&!username.equals("")){
			hql.append(" And u.username = ? ");
			param.add(username);
		}
		return  (List<UserInfo>) this.queryForPage(hql.toString(), param, offset, length);
	}

	public int loadAllUserCount(String username) {
		StringBuffer hql = new StringBuffer("Select u from UserInfo u where 1=1 ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(username!=null&&!username.equals("")){
			hql.append(" And u.username = ? ");
			param.add(username);
		}
		return this.getAllRowCount(hql.toString(), param);
	}
}
